package com.wetao.module_report.presenter;

import android.content.Context;

import com.google.gson.Gson;

import com.wetao.lib_common.base.BaseResponse;

import com.wetao.lib_common.bean.response.SingleStudyInfo;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.ToastUtil;
import com.wetao.module_report.contract.ReportSingleDetailCon;


import java.util.HashMap;


import okhttp3.RequestBody;


/**
 * author: zhouyh
 * created on: 2019-11-15 16:33
 * description:
 */
public class ReportSingleDetailPre extends ReportSingleDetailCon.Presenter {


    private Context mContext;

    public ReportSingleDetailPre(Context mContext) {
        this.mContext = mContext;
    }



    @Override
    public void querySingleStudyInfo(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().querySingleStudyInfo(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<SingleStudyInfo>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<SingleStudyInfo>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<SingleStudyInfo>>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse<SingleStudyInfo> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0 || baseResponse.getCode() == 1){
                            getView().querySingleStudyInfoResult(baseResponse);
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            getView().querySingleStudyInfoFailed();
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(e.getMessage());
                        getView().querySingleStudyInfoFailed();
                    }
                });
    }


}
